import os
import argparse

HELP = "FCINEMA - python & php opensource web server for watch MP4/WEBM movies or just videos"


#---------------functions------------------

def list_movies(i):
    movies = os.listdir("videos/")
    while i < len(movies):
        print(movies[i])
        i=i+1


def delete_movie(movie):
        movie_ = os.path.splitext(movie)[0]
        os.system("rm videos/" + movie)
        os.system("rm pages/" + movie_ + ".html")
        os.system("rm prev/" + movie_ + ".*")


def start_srv(address):
    os.system("php -S " + address + " -t .")


def add_movie(path, name, poster):
    #проверка 
    if not os.path.isfile(path):
        print(f"{path} dont exists")
        exit(0)
    if not os.path.isfile(poster):
        print(f"{poster} dont exists")
        exit(0)

    #получаем имена файлов и расширения
    poster_ext = os.path.splitext(poster)[1]   #.jpg .png etc
    poster_filename = os.path.basename(poster)  #poster.jpg
    filename_full = os.path.basename(path)   #filename.mp4
    filename = os.path.splitext(filename_full)[0]   #filename
    file_ext = os.path.splitext(filename_full)[1]   #.mp4
    file_ext_without = file_ext.replace(".", "")   #mp4
    
    #копируем постер и фильм
    os.system("cp "+ path + " $PWD/videos/ -v")
    os.system("cp " + poster + " $PWD/prev/ -v ")
    mv = "mv $PWD/prev/" + poster_filename + " " + "$PWD/prev/" + filename + poster_ext
    os.system(mv)

    #заменяем из example.txt и записываем в contents
    with open("pages/example.txt", "r+") as file:
        contents = file.read()
        contents = contents.replace("__MOVIENAME__", name)
        contents = contents.replace("__FILENAME__", filename_full) 
        contents = contents.replace("__FILEEXT__", file_ext_without)
        file.close
    
    #если html с таким же именем already exists, удаляем
    path2html = "pages/" + filename + ".html"     
    
    if os.path.isfile(path2html):
        os.system("rm " + path2html)    
    
    #записываем в html файл contents
    with open(path2html, "w") as file:
        file.write(contents)
        file.close
    
#-------------------------------------------------------------------


#---------------MAIN-----------------

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description=HELP)
    parser.add_argument('-s', '--server', type=str, help='start php server. takes one arg - IP:PORT')
    parser.add_argument('-a', '--add', type=str, nargs=3, help='add video. takes three args - path to videofile, name, path to poster')
    parser.add_argument('-d', '--delete', type=str, help='delete video from server. take one arg - filename')
    parser.add_argument('-l', '--list', action='store_true', help='list all videos on server')
    args = parser.parse_args()


    if args.server is not None:
        start_srv(args.server)
    if args.add:
        add_movie(args.add[0], args.add[1],args.add[2])
    if args.delete:
        delete_movie(args.delete)
    if args.list:
        list_movies(0)

